package controllers;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class Utils {
	public static String fomatDmain(String url)
	{
		if (url != null)
			url = url.toLowerCase().replaceAll("(http|https)+://(www.)*", "")
					.replaceAll("^[\\w\\W]+[\\/]{2}", "")
					.replaceAll("[\\/]+[\\w\\W]*$", "").replaceAll("www.", "")
					.replaceAll("[\\?]+[\\w\\W]*$", "")
					.replaceAll("[ !@#$%^&*?()\\[\\]{}+=,:;|\\\'\\\"/\\\\`~]+", "")
					.trim();

		return url;
	}
	public static String fomatDmains(String url)
	{
		if (url != null)
			url = url.toLowerCase().replaceAll("(http|https)+://", "")
					.replaceAll("^[\\w\\W]+[\\/]{2}", "")
					.replaceAll("[\\/]+[\\w\\W]*$", "")
					.replaceAll("[\\?]+[\\w\\W]*$", "")
					.replaceAll("[ !@#$%^&*?()\\[\\]{}+=,:;|\\\'\\\"/\\\\`~]+", "")
					.trim();

		return url;
	}
	public static String fomatSitemap(String url)
	{
		if (url != null)
			url = "http://"+url.toLowerCase().replaceAll("(http|https)+://", "").trim().replaceAll("[/]+$", "");
		return url;
	}
	public static void checkLoginTrue(HttpServletRequest request,HttpServletResponse response)
	{
		HttpSession session = request.getSession();
		if(session.getAttribute("users")!=null)
		{
			try {
				response.sendRedirect("/add-link");
				return;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void checkLoginFalse(HttpServletRequest request,HttpServletResponse response)
	{
		HttpSession session = request.getSession();
		if(session.getAttribute("users")==null)
		{
			try {
				response.sendRedirect("/login");
				return;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static String cleanContent(String str) {
		str = str.replaceAll(
				"(class|id)[ ]*[=]{1}[ ]*[\\'\\\"]{1}[^\\'\\\"]+[\\'\\\"]{1}",
				"");
		str = str.replaceAll("<script[^<]+</script[ ]*>", "");
		return str;
	}
}
